Parameterized Tests in Selenium and TestNG
Parameterized Testing is a testing technique in which the same test logic is executed multiple times using different input values. Instead of creating separate test methods for every combination of test data, parameters allow the test framework to supply different values to a reusable test method.
In Selenium automation, parameterized tests are commonly used for testing different usernames, passwords, browsers, URLs, environments, search keywords, user roles, product information, expected results, and other test conditions. TestNG provides features such as @Parameters and @DataProvider for implementing parameterized testing.
Parameterized testing is an important part of data-driven automation because it separates reusable test logic from the values used during test execution.
Course Resource: Selenium Training | Register for Course Demo
1. What are Parameterized Tests?
Parameterized tests are tests that receive input values from an external or configurable source instead of using fixed values directly inside the test method.
The same test method can therefore execute multiple times with different parameters.
Test Method
|
+---- Test Data 1
|
+---- Test Data 2
|
+---- Test Data 3
|
+---- Test Data 4
|
v
Multiple Test Executions
For example, instead of creating separate login methods for three users, a single login test can receive different username and password values.
2. Why are Parameterized Tests Important?
Real-world applications need to be tested with many different inputs. Parameterization allows automation engineers to reuse the same test logic while changing only the test data.
- Reduces duplicate test code.
- Improves test reusability.
- Supports data-driven testing.
- Improves maintainability.
- Allows broader test coverage.
- Makes test data easier to manage.
- Supports multiple environments.
- Supports cross-browser testing.
- Works well with Selenium WebDriver.
- Can be integrated with Page Object Model.
- Can be combined with Maven and CI/CD.
- Supports positive and negative testing scenarios.
3. Parameterized Test Flow
Test Data Source
|
v
Parameter / DataProvider
|
v
Parameterized Test Method
|
v
Selenium WebDriver
|
v
Application
|
v
Validation / Assertion
|
v
Test Result
The test framework supplies a particular set of values to the test method, executes the test, and then repeats the process with the next set of values.
4. Parameterization in Selenium Testing
Selenium WebDriver performs browser automation, while TestNG can provide the values required by the Selenium test.
For example, Selenium can automate a login page while TestNG provides different usernames and passwords.
TestNG
|
+-- Username
+-- Password
|
v
Selenium Test
|
v
Login Page
|
v
Enter Credentials
|
v
Click Login
|
v
Validate Result
5. TestNG Parameterization Techniques
TestNG provides multiple mechanisms for parameterized testing.
| Technique | Purpose | Typical Use |
| @Parameters | Pass named configuration values | Browser, URL, environment |
| @DataProvider | Supply multiple test-data sets | Login, search, forms |
| @Optional | Provide a default parameter value | Optional configuration |
| External Data | Read values from files or databases | Large test-data sets |
6. @Parameters Annotation
The @Parameters annotation allows TestNG to receive named parameters from the TestNG XML configuration file.
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class LoginTest {
@Test
@Parameters({"username", "password"})
public void loginTest(String username, String password) {
System.out.println(username);
System.out.println(password);
}
}
The parameter names specified in @Parameters must correspond to the names defined in the TestNG XML configuration.
7. Defining Parameters in testng.xml
<suite name="Automation Suite">
<test name="Login Test">
<parameter name="username" value="admin"/>
<parameter name="password" value="admin123"/>
<classes>
<class name="LoginTest"/>
</classes>
</test>
</suite>
TestNG reads these values and passes them to the test method.
8. Complete @Parameters Example
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParameterTest {
@Test
@Parameters({"browser", "url"})
public void testApplication(String browser, String url) {
System.out.println("Browser: " + browser);
System.out.println("URL: " + url);
}
}
TestNG XML:
<suite name="Suite">
<test name="Application Test">
<parameter name="browser" value="chrome"/>
<parameter name="url" value="https://example.com"/>
<classes>
<class name="ParameterTest"/>
</classes>
</test>
</suite>
9. Parameterizing Browser Name
Browser parameterization is useful when the same Selenium test needs to run on different browsers.
@Test
@Parameters("browser")
public void browserTest(String browser) {
System.out.println("Executing test on: " + browser);
}
The XML configuration can provide values such as Chrome, Firefox, or Edge.
10. Cross-Browser Parameterization
Cross-browser testing verifies that an application behaves correctly across different browsers.
<suite name="Cross Browser Suite">
<test name="Chrome Test">
<parameter name="browser" value="chrome"/>
<classes>
<class name="BrowserTest"/>
</classes>
</test>
<test name="Firefox Test">
<parameter name="browser" value="firefox"/>
<classes>
<class name="BrowserTest"/>
</classes>
</test>
<test name="Edge Test">
<parameter name="browser" value="edge"/>
<classes>
<class name="BrowserTest"/>
</classes>
</test>
</suite>
11. Browser Parameter with WebDriver
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.BeforeMethod;
public class BrowserTest {
WebDriver driver;
@BeforeMethod
@Parameters("browser")
public void setup(String browser) {
if (browser.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("edge")) {
driver = new EdgeDriver();
} else {
throw new IllegalArgumentException("Unsupported browser: " + browser);
}
}
}
12. Parameterizing Application URL
URLs are often parameterized so that the same test can execute against different environments.
@Test
@Parameters("url")
public void openApplication(String url) {
driver.get(url);
System.out.println("Application URL: " + url);
}
Example environments can include:
| Environment | Example URL |
| Development | https://dev.example.com |
| QA | https://qa.example.com |
| Staging | https://stage.example.com |
| Production | https://www.example.com |
13. Environment Parameterization
Environment parameterization allows the same test suite to run against different application environments without modifying the test source code.
@Test
@Parameters({"environment", "url"})
public void environmentTest(String environment, String url) {
System.out.println("Environment: " + environment);
System.out.println("URL: " + url);
driver.get(url);
}
14. Environment-Based Selenium Test
<suite name="Environment Suite">
<test name="QA Test">
<parameter name="environment" value="QA"/>
<parameter name="url" value="https://qa.example.com"/>
<classes>
<class name="EnvironmentTest"/>
</classes>
</test>
</suite>
The test source code remains unchanged while the execution environment can be changed through configuration.
15. Parameterizing Username and Password
Login tests are a common use case for parameterization.
@Test
@Parameters({"username", "password"})
public void loginTest(String username, String password) {
driver.findElement(By.id("username"))
.sendKeys(username);
driver.findElement(By.id("password"))
.sendKeys(password);
driver.findElement(By.id("loginButton"))
.click();
}
For real projects, sensitive credentials should preferably be supplied through secure configuration or secret-management systems rather than being stored as plain text in source-controlled XML.
16. Parameterizing Search Data
Search functionality can use parameters for different keywords.
@Test
@Parameters("searchText")
public void searchTest(String searchText) {
driver.findElement(By.id("search"))
.sendKeys(searchText);
driver.findElement(By.id("searchButton"))
.click();
}
17. Parameterizing Test Data
Parameterized tests can receive multiple values such as name, email, mobile number, role, and expected result.
@Test
@Parameters({"name", "email", "role"})
public void userTest(
String name,
String email,
String role) {
System.out.println(name);
System.out.println(email);
System.out.println(role);
}
18. @Parameters vs @DataProvider
| Feature | @Parameters | @DataProvider |
| Main Purpose | Configuration values | Test data |
| Data Source | testng.xml | Java method or external source |
| Multiple Data Sets | Not its primary purpose | Yes |
| Browser Configuration | Very suitable | Possible |
| Login Combinations | Limited | Very suitable |
| Data-Driven Testing | Limited | Designed for it |
| Repeated Invocation | Not inherently | Yes |
19. DataProvider for Parameterized Tests
When a test needs multiple sets of values, TestNG's @DataProvider is generally more suitable.
@DataProvider(name = "loginData")
public Object[][] loginData() {
return new Object[][] {
{"admin", "admin123"},
{"manager", "manager123"},
{"employee", "employee123"}
};
}
@Test(dataProvider = "loginData")
public void loginTest(String username, String password) {
System.out.println(username);
System.out.println(password);
}
20. Parameterized Tests with Expected Results
Expected results can be supplied along with input data.
@DataProvider(name = "calculatorData")
public Object[][] calculatorData() {
return new Object[][] {
{10, 20, 30},
{5, 5, 10},
{100, 50, 150}
};
}
@Test(dataProvider = "calculatorData")
public void additionTest(
int a,
int b,
int expected) {
int actual = a + b;
Assert.assertEquals(actual, expected);
}
21. Parameterized Positive and Negative Tests
Parameterized testing is useful for validating both valid and invalid input combinations.
@DataProvider(name = "loginScenarios")
public Object[][] loginScenarios() {
return new Object[][] {
{"validUser", "validPass", "success"},
{"invalidUser", "validPass", "invalid username"},
{"validUser", "invalidPass", "invalid password"},
{"", "", "required fields"}
};
}
@Test(dataProvider = "loginScenarios")
public void loginValidationTest(
String username,
String password,
String expectedResult) {
System.out.println("Expected: " + expectedResult);
}
22. Parameterized Tests with Multiple Parameters
A single test can accept several parameters.
@Test
@Parameters({"username", "password", "role", "status"})
public void userTest(
String username,
String password,
String role,
String status) {
System.out.println(username);
System.out.println(password);
System.out.println(role);
System.out.println(status);
}
23. Parameter Scope in TestNG
TestNG parameters can be defined at different levels. A parameter can be configured at suite level or test level depending on how it needs to be shared.
<suite name="Suite">
<parameter name="browser" value="chrome"/>
<test name="Test">
<classes>
<class name="LoginTest"/>
</classes>
</test>
</suite>
24. Suite-Level Parameter
A suite-level parameter can provide a common value to tests in the suite unless a more specific parameter overrides it.
<suite name="Automation Suite">
<parameter name="browser" value="chrome"/>
<test name="Login Test">
<classes>
<class name="LoginTest"/>
</classes>
</test>
</suite>
25. Test-Level Parameter
A test-level parameter can be defined inside a specific <test> element.
<suite name="Suite">
<test name="Firefox Test">
<parameter name="browser" value="firefox"/>
<classes>
<class name="BrowserTest"/>
</classes>
</test>
</suite>
26. Overriding Parameters
More specific parameter definitions can override broader values. This is useful when most tests use one configuration but a particular test requires another.
<suite name="Suite">
<parameter name="browser" value="chrome"/>
<test name="Firefox Test">
<parameter name="browser" value="firefox"/>
<classes>
<class name="BrowserTest"/>
</classes>
</test>
</suite>
27. @Optional Parameter
TestNG provides the @Optional annotation for supplying a default value when a named parameter is not provided.
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
@Test
@Parameters("browser")
public void browserTest(
@Optional("chrome") String browser) {
System.out.println("Browser: " + browser);
}
If the browser parameter is not supplied, the test can use Chrome as the default value.
28. Parameterized @BeforeMethod
Configuration methods can also receive parameters.
@BeforeMethod
@Parameters("browser")
public void setup(String browser) {
System.out.println(
"Starting browser: " + browser
);
}
29. Parameterized @BeforeTest
@BeforeTest
@Parameters("environment")
public void setupEnvironment(String environment) {
System.out.println(
"Environment: " + environment
);
}
This can be useful when test setup depends on an environment or configuration value.
30. Parameterized @BeforeSuite
@BeforeSuite
@Parameters("browser")
public void suiteSetup(String browser) {
System.out.println(
"Suite Browser: " + browser
);
}
The parameter must be available within the relevant configuration scope.
31. Parameterized Page Object Model
Parameterized tests work effectively with the Page Object Model. Test data is supplied to the test layer while page classes handle application interactions.
Test Data
|
v
Parameterized Test
|
v
Page Object
|
v
WebDriver
|
v
Application
32. POM Login Example
public class LoginPage {
private WebDriver driver;
private By username =
By.id("username");
private By password =
By.id("password");
private By loginButton =
By.id("loginButton");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(
String user,
String pass) {
driver.findElement(username)
.sendKeys(user);
driver.findElement(password)
.sendKeys(pass);
driver.findElement(loginButton)
.click();
}
}
33. POM Test with DataProvider
@DataProvider(name = "loginData")
public Object[][] loginData() {
return new Object[][] {
{"admin", "admin123"},
{"manager", "manager123"},
{"employee", "employee123"}
};
}
@Test(dataProvider = "loginData")
public void loginTest(
String username,
String password) {
LoginPage loginPage =
new LoginPage(driver);
loginPage.login(
username,
password
);
}
34. Parameterized Search Test
@DataProvider(name = "searchData")
public Object[][] searchData() {
return new Object[][] {
{"Laptop"},
{"Mobile"},
{"Headphones"},
{"Keyboard"},
{"Mouse"}
};
}
@Test(dataProvider = "searchData")
public void searchTest(String keyword) {
driver.findElement(By.id("search"))
.clear();
driver.findElement(By.id("search"))
.sendKeys(keyword);
driver.findElement(By.id("searchButton"))
.click();
}
35. Parameterized Registration Test
@DataProvider(name = "registrationData")
public Object[][] registrationData() {
return new Object[][] {
{"John", "[email protected]", "9876543210"},
{"David", "[email protected]", "9876543211"},
{"Robert", "[email protected]", "9876543212"}
};
}
@Test(dataProvider = "registrationData")
public void registrationTest(
String name,
String email,
String mobile) {
System.out.println(name);
System.out.println(email);
System.out.println(mobile);
}
36. Parameterized E-Commerce Test
E-commerce automation frequently requires different products, quantities, categories, and discount combinations.
@DataProvider(name = "productData")
public Object[][] productData() {
return new Object[][] {
{"Laptop", 1},
{"Mobile", 2},
{"Headphones", 3}
};
}
@Test(dataProvider = "productData")
public void productTest(
String product,
int quantity) {
System.out.println(product);
System.out.println(quantity);
}
37. Parameterized User Role Testing
@DataProvider(name = "roles")
public Object[][] roles() {
return new Object[][] {
{"Admin"},
{"Manager"},
{"Employee"},
{"Customer"}
};
}
@Test(dataProvider = "roles")
public void roleTest(String role) {
System.out.println(
"Testing role: " + role
);
}
38. Parameterized Browser Testing with DataProvider
@DataProvider(name = "browsers")
public Object[][] browsers() {
return new Object[][] {
{"chrome"},
{"firefox"},
{"edge"}
};
}
@Test(dataProvider = "browsers")
public void browserTest(String browser) {
System.out.println(
"Running on: " + browser
);
}
For a complete browser framework, the browser value can be passed to a reusable DriverFactory.
39. Parameterized Environment Testing
@DataProvider(name = "environments")
public Object[][] environments() {
return new Object[][] {
{"QA", "https://qa.example.com"},
{"Stage", "https://stage.example.com"},
{"Production", "https://www.example.com"}
};
}
@Test(dataProvider = "environments")
public void environmentTest(
String environment,
String url) {
System.out.println(environment);
System.out.println(url);
}
40. External Test Data
For large projects, test data does not always need to be hard-coded inside Java classes. A parameterized test can obtain its data from external sources.
- Excel files.
- CSV files.
- JSON files.
- Properties files.
- Databases.
- APIs.
- Environment variables.
- Secure secret-management systems.
41. Parameterized Tests with Excel
Apache POI can be used to read Excel files and convert rows into data supplied by a DataProvider.
@DataProvider(name = "excelData")
public Object[][] excelData() {
// Excel reading logic can be implemented here.
return new Object[][] {
{"user1", "pass1"},
{"user2", "pass2"},
{"user3", "pass3"}
};
}
42. Parameterized Tests with CSV
@DataProvider(name = "csvData")
public Object[][] csvData() {
// CSV reading logic can be implemented here.
return new Object[][] {
{"John", "[email protected]"},
{"David", "[email protected]"}
};
}
43. Parameterized Tests with JSON
JSON is frequently used for test data in modern automation frameworks.
@DataProvider(name = "jsonData")
public Object[][] jsonData() {
return new Object[][] {
{"Chrome", "https://example.com"},
{"Firefox", "https://example.com"}
};
}
44. Parameterized Tests with Database Data
Database-driven parameterization can retrieve records dynamically and pass them to test methods.
@DataProvider(name = "databaseData")
public Object[][] databaseData() {
// Database connection and query
// logic can be implemented here.
return new Object[][] {
{"user1", "active"},
{"user2", "inactive"}
};
}
45. Parameterized Tests with Assertions
Expected results can be included in test data and compared against actual application behavior.
@DataProvider(name = "calculatorData")
public Object[][] calculatorData() {
return new Object[][] {
{10, 20, 30},
{5, 5, 10},
{100, 25, 125}
};
}
@Test(dataProvider = "calculatorData")
public void additionTest(
int a,
int b,
int expected) {
int actual = a + b;
Assert.assertEquals(
actual,
expected
);
}
46. Parameterized Tests and Test Reports
Each invocation of a parameterized test can be represented as a separate test execution in TestNG reporting systems.
LoginTest
|
|-- admin / admin123 PASS
|-- manager / manager123 PASS
|-- invalid / wrong123 FAIL
Test reports should make it possible to identify which test data caused a failure. Sensitive values such as passwords should not be exposed in reports or logs.
47. Parameterized Tests with Maven
Parameterized TestNG tests can be executed through Maven like regular TestNG tests.
mvn test
Maven can manage dependencies, build the project, execute the tests, and integrate results with CI/CD systems.
48. Parameterized Tests in CI/CD
Parameterized tests are useful in CI/CD because the same automation suite can execute with different configurations and data sets.
Developer Commit
|
v
CI/CD Pipeline
|
v
Maven Build
|
v
TestNG
|
v
Parameterized Tests
|
v
Selenium WebDriver
|
v
Application
|
v
Reports
49. Parameterized Tests with Jenkins
Jenkins can trigger Maven-based Selenium TestNG suites. Parameters can be supplied through pipeline configuration, environment variables, Maven properties, or TestNG configuration depending on the framework architecture.
Jenkins
|
v
Build
|
v
Maven
|
v
TestNG
|
v
Parameterized Test
|
v
Selenium
|
v
Test Report
50. Parameterization and Parallel Execution
Parameterized test invocations can be executed in parallel when the framework is designed for thread safety.
@DataProvider(
name = "users",
parallel = true
)
public Object[][] users() {
return new Object[][] {
{"user1"},
{"user2"},
{"user3"},
{"user4"}
};
}
Parallel execution should be used carefully because concurrent Selenium tests should not share WebDriver instances or mutable state unsafely.
51. Thread Safety in Parameterized Selenium Tests
When multiple parameterized tests execute simultaneously, each test should generally have an isolated browser session and isolated test state.
Data Provider
|
+---- Thread 1 ---- WebDriver 1
|
+---- Thread 2 ---- WebDriver 2
|
+---- Thread 3 ---- WebDriver 3
|
v
Independent Test Execution
A common framework approach is to use a thread-safe DriverFactory or ThreadLocal-based driver management when parallel execution requires it.
52. Parameterized Tests with Driver Factory
A DriverFactory can centralize browser creation while parameterized tests provide the browser value.
public class DriverFactory {
public static WebDriver createDriver(
String browser) {
if (browser.equalsIgnoreCase("chrome")) {
return new ChromeDriver();
}
if (browser.equalsIgnoreCase("firefox")) {
return new FirefoxDriver();
}
if (browser.equalsIgnoreCase("edge")) {
return new EdgeDriver();
}
throw new IllegalArgumentException(
"Unsupported browser: " + browser
);
}
}
53. Parameterized Test Execution Flow
TestNG Starts
|
v
Read Parameters
|
v
Create Test Configuration
|
v
Create WebDriver
|
v
Execute Test
|
v
Perform Assertions
|
v
Capture Result
|
v
Execute Next Data Set
|
v
Generate Report
54. Parameterized Test vs Hard-Coded Test
| Hard-Coded Test | Parameterized Test |
| Values are directly written in test code | Values are supplied externally or through data mechanisms |
| More duplication | Less duplication |
| Difficult to scale | Easy to expand |
| Lower reusability | Higher reusability |
| Changing data may require code changes | Data can often be changed independently |
55. Parameterized Test vs Data-Driven Testing
Parameterized testing is the mechanism of supplying variable inputs to reusable test logic. Data-driven testing is a broader testing approach where test data is separated from test logic and multiple data sets are used systematically.
TestNG DataProviders are one common implementation of data-driven parameterization.
56. Parameterized Test vs @Parameters
The term parameterized test can refer broadly to tests receiving variable values. TestNG's @Parameters is specifically designed for named parameters, often configuration-oriented values from XML.
For example:
@Parameters({"browser", "url"})
public void test(String browser, String url) {
}
This is different from a DataProvider that supplies many rows of test data.
57. Parameterized Tests for Regression Testing
Regression testing often requires the same workflow to be validated with different inputs after application changes.
For example, a search regression test can use many search keywords while keeping the test logic unchanged.
Search Test
|
+-- Laptop
+-- Mobile
+-- Tablet
+-- Headphones
+-- Keyboard
+-- Mouse
58. Parameterized Tests for E-Commerce
E-commerce applications provide many practical parameterization scenarios.
| Scenario | Possible Parameters |
| Login | Username, password |
| Search | Keyword |
| Product | Product name, quantity |
| Filters | Category, price range, brand |
| Checkout | Address, payment method |
| Discount | Coupon code |
| User Role | Admin, manager, customer |
59. Parameterized Tests for API and UI Testing
The same parameterized test-data concepts can be used across UI and API automation. For example, a set of user records can be used to validate API responses and corresponding UI behavior.
Common Test Data
|
+---- API Test
|
+---- UI Test
|
+---- Database Validation
|
v
Reusable Test Data
60. Common Mistakes in Parameterized Tests
- Using incorrect parameter names.
- Using the wrong DataProvider name.
- Mismatch between parameter count and supplied values.
- Using incompatible Java data types.
- Hard-coding sensitive credentials.
- Sharing WebDriver instances between parallel tests.
- Putting excessive business logic inside DataProviders.
- Creating unnecessarily large in-memory data sets.
- Failing to identify which parameter set caused a failure.
- Using parameterization where simple configuration is sufficient.
61. Parameter Name Mismatch
The name referenced in the test method must match the configured parameter name.
Incorrect configuration:
<parameter name="browserName" value="chrome"/>
Incorrect Java code:
@Parameters("browser")
public void test(String browser) {
}
Correct Java code:
@Parameters("browserName")
public void test(String browser) {
}
62. Parameter Count Mismatch
When using multiple parameters, the test method should accept the expected number of values.
@Parameters({"username", "password", "role"})
public void test(
String username,
String password,
String role) {
}
If the configuration does not supply the required values, TestNG may report a parameter-related execution error.
63. Handling Unsupported Browser Values
Browser parameters should be validated before creating a WebDriver instance.
if (browser.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("edge")) {
driver = new EdgeDriver();
} else {
throw new IllegalArgumentException(
"Unsupported browser: " + browser
);
}
64. Handling Null or Empty Parameters
Tests should validate required parameters before using them.
if (username == null || username.trim().isEmpty()) {
throw new IllegalArgumentException(
"Username cannot be empty"
);
}
Proper validation helps produce clearer failures and makes debugging easier.
65. Parameterized Tests and Logging
Logging should identify the important test parameters so that failed executions can be investigated.
System.out.println(
"Executing login test for user: "
+ username
);
Do not log passwords, authentication tokens, API keys, or other secrets in plain text.
66. Parameterized Tests and Reporting
Parameterized executions should be clearly identifiable in reports. A useful report can show the test name, browser, environment, input category, status, duration, and failure reason.
| Test | Browser | Environment | Status |
| Login | Chrome | QA | PASS |
| Login | Firefox | QA | PASS |
| Login | Edge | QA | FAIL |
67. Best Practices for Parameterized Tests
- Keep test logic independent from test data wherever practical.
- Use meaningful parameter names.
- Use DataProviders for multiple test-data combinations.
- Use @Parameters for configuration-style values.
- Keep sensitive credentials outside source-controlled test data.
- Use reusable DriverFactory components.
- Use Page Object Model for Selenium interactions.
- Validate parameter values before execution.
- Use assertions to validate expected results.
- Keep external test data organized.
- Identify parameter values in reports without exposing secrets.
- Use parallel execution only when the framework is thread-safe.
- Keep parameterized tests small and focused.
- Use meaningful test data that represents real scenarios.
68. Practical Selenium Project Structure
src
|-- test
|-- java
|-- tests
| |-- LoginTest.java
| |-- SearchTest.java
| |-- CheckoutTest.java
|
|-- pages
| |-- LoginPage.java
| |-- SearchPage.java
| |-- CheckoutPage.java
|
|-- data
| |-- LoginDataProvider.java
| |-- SearchDataProvider.java
| |-- ProductDataProvider.java
|
|-- utilities
|-- DriverFactory.java
|-- ExcelReader.java
|-- ConfigReader.java
69. Complete Practical Parameterized Login Example
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class LoginTest {
WebDriver driver;
@BeforeMethod
public void setup() {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com/login");
}
@DataProvider(name = "loginData")
public Object[][] loginData() {
return new Object[][] {
{"admin", "admin123"},
{"manager", "manager123"},
{"employee", "employee123"}
};
}
@Test(dataProvider = "loginData")
public void loginTest(
String username,
String password) {
driver.findElement(
By.id("username")
).sendKeys(username);
driver.findElement(
By.id("password")
).sendKeys(password);
driver.findElement(
By.id("loginButton")
).click();
Assert.assertTrue(
driver.getTitle().contains("Dashboard")
);
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
70. Complete Browser Parameterization Example
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class CrossBrowserTest {
WebDriver driver;
@BeforeMethod
@Parameters("browser")
public void setup(String browser) {
if (browser.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("edge")) {
driver = new EdgeDriver();
} else {
throw new IllegalArgumentException(
"Unsupported browser: " + browser
);
}
driver.manage().window().maximize();
}
@Test
@Parameters("url")
public void applicationTest(String url) {
driver.get(url);
System.out.println(
"Testing URL: " + url
);
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
71. TestNG XML for Cross-Browser Execution
<suite name="Cross Browser Suite">
<test name="Chrome Test">
<parameter name="browser" value="chrome"/>
<parameter name="url" value="https://example.com"/>
<classes>
<class name="CrossBrowserTest"/>
</classes>
</test>
<test name="Firefox Test">
<parameter name="browser" value="firefox"/>
<parameter name="url" value="https://example.com"/>
<classes>
<class name="CrossBrowserTest"/>
</classes>
</test>
<test name="Edge Test">
<parameter name="browser" value="edge"/>
<parameter name="url" value="https://example.com"/>
<classes>
<class name="CrossBrowserTest"/>
</classes>
</test>
</suite>
72. Real-World Parameterized Test Architecture
Test Configuration
|
v
+---------+---------+
| |
Browser URL
| |
+---------+---------+
|
v
TestNG Test
|
v
Data Provider
|
v
Test Method
|
v
Page Objects
|
v
Selenium WebDriver
|
v
Application
|
v
Assertions
|
v
Reports
73. Parameterized Testing in a Complete Automation Framework
A mature Selenium framework can combine parameterization with several automation components.
| Component | Responsibility |
| TestNG | Test execution and parameter management |
| DataProvider | Multiple test-data sets |
| @Parameters | Configuration values |
| Page Object Model | Page interaction logic |
| DriverFactory | Browser creation |
| Excel/CSV/JSON | External test data |
| Maven | Build and dependency management |
| Jenkins | CI/CD execution |
| ExtentReports/Allure | Test reporting |
| Git/GitHub | Version control |
74. Advantages of Parameterized Tests
- Reusability: One test method can handle multiple scenarios.
- Maintainability: Changes to test data do not necessarily require changes to test logic.
- Scalability: Additional test data can be added easily.
- Coverage: More combinations can be tested.
- Less Duplication: Similar test methods are avoided.
- Flexibility: Tests can run across browsers and environments.
- Automation Integration: Parameterization works with Selenium, TestNG, Maven, POM, CI/CD, and reporting.
- Data-Driven Testing: External data sources can be incorporated into the framework.
75. Limitations of Parameterized Tests
- Very large test-data sets may increase memory usage when loaded into arrays.
- Complex external data sources require additional utilities.
- Incorrect parameter mapping can cause execution failures.
- Parallel execution requires thread-safe framework design.
- Poorly organized test data can make failures difficult to diagnose.
- Sensitive data requires secure handling.
- Not every test requires parameterization.
76. Common Interview Questions on Parameterized Tests
1. What is a parameterized test?
A parameterized test is a reusable test method that receives different input values during different executions.
2. Why is parameterization useful in Selenium?
It allows the same Selenium workflow to be tested with different browsers, credentials, URLs, search values, roles, and other inputs.
3. Which TestNG annotation is used for XML parameters?
The @Parameters annotation is used to receive named parameters from TestNG configuration.
4. What is @DataProvider?
@DataProvider is a TestNG annotation used to supply multiple sets of test data to a test method.
5. What is the difference between @Parameters and @DataProvider?
@Parameters is commonly used for configuration-style values, while @DataProvider is designed for multiple test-data sets and data-driven execution.
6. Can Selenium tests be parameterized?
Yes. Selenium tests can receive parameters through TestNG and use those values during browser automation.
7. Can browser names be parameterized?
Yes. Browser names can be supplied through @Parameters or DataProvider and passed to a DriverFactory.
8. Can URLs be parameterized?
Yes. URLs are commonly parameterized to execute tests against different environments.
9. Can parameterized tests be used with POM?
Yes. Test data can be supplied to test classes while page objects manage application interactions.
10. Can DataProviders execute tests in parallel?
Yes. A DataProvider can use parallel = true, provided the automation framework is thread-safe.
11. What is @Optional?
@Optional allows a default value to be specified when a TestNG parameter is not supplied.
12. Can external files be used for parameterized tests?
Yes. Excel, CSV, JSON, databases, and other sources can provide test data.
13. Why should passwords not be hard-coded?
Passwords and other secrets should not normally be stored as plain text in source-controlled automation code or reports.
14. How does parameterization improve maintainability?
It separates reusable test logic from variable input values, reducing duplicate test code.
15. What happens if a parameter is missing?
Depending on the configuration and annotation usage, TestNG can report a missing parameter error or use a value supplied through @Optional.
16. What is cross-browser parameterization?
It is the practice of supplying browser names as test configuration or test data so that the same test can run against multiple browsers.
17. What is environment parameterization?
It is the practice of supplying environment-specific values such as QA, staging, or production URLs to reusable tests.
18. Can parameterized tests be integrated with Maven?
Yes. Maven can execute TestNG-based parameterized tests as part of the project build.
19. Can parameterized tests be used in CI/CD?
Yes. Parameterized Selenium suites can run in CI/CD pipelines with different environments and configurations.
20. What is the biggest benefit of parameterized testing?
The major benefit is that reusable test logic can validate many input combinations without creating duplicate test methods.
77. Quick Reference Table
| Concept | Purpose |
| Parameterized Test | Runs reusable test logic with variable inputs |
| @Parameters | Receives named TestNG parameters |
| @DataProvider | Provides multiple test-data sets |
| @Optional | Provides a default parameter value |
| testng.xml | Defines TestNG configuration and parameters |
| POM | Separates page interaction logic from tests |
| DriverFactory | Creates browser-specific WebDriver instances |
| Excel | External test-data source |
| CSV | External test-data source |
| JSON | Structured external test-data source |
| Database | Dynamic test-data source |
| parallel = true | Enables parallel DataProvider invocations |
78. Learning Roadmap for Parameterized Tests
- Learn TestNG fundamentals.
- Understand TestNG annotations.
- Learn the @Parameters annotation.
- Understand testng.xml parameters.
- Learn parameter scope and overriding.
- Learn @Optional.
- Understand @DataProvider.
- Create single-column DataProviders.
- Create multiple-column DataProviders.
- Use expected values with parameterized tests.
- Combine parameterization with Selenium.
- Combine parameterization with Page Object Model.
- Build a reusable DriverFactory.
- Read test data from external files.
- Implement cross-browser parameterization.
- Implement environment parameterization.
- Learn parallel parameterized execution.
- Integrate Maven and CI/CD.
- Generate parameter-aware reports.
- Build a complete data-driven Selenium framework.
79. Practical Exercises
- Create a TestNG test that accepts a browser parameter.
- Create a test that accepts an application URL.
- Create a login test using username and password parameters.
- Create a search test with multiple keywords using DataProvider.
- Create a registration test with multiple user records.
- Create a browser DataProvider for Chrome, Firefox, and Edge.
- Create an environment DataProvider for QA and staging.
- Create a role-based test for Admin, Manager, and Employee.
- Create a calculator test with input and expected-result parameters.
- Read parameterized test data from Excel.
- Create a reusable DataProvider class.
- Integrate parameterization with Page Object Model.
- Execute parameterized tests in parallel.
- Generate a report that identifies the parameter values used by each test.
- Integrate the parameterized framework with Maven and Jenkins.
80. Real-World Parameterized Selenium Scenario
Consider an e-commerce application where the QA team needs to test login functionality on multiple browsers and search functionality with multiple products.
Browser Data
|
+-- Chrome
+-- Firefox
+-- Edge
|
v
Browser Configuration
|
v
Selenium WebDriver
|
v
Login Test
|
+-- Admin
+-- Manager
+-- Employee
|
v
Search Test
|
+-- Laptop
+-- Mobile
+-- Tablet
+-- Headphones
|
v
Assertions
|
v
Test Report
Instead of creating separate test methods for every combination, the framework can reuse the same automation logic and change the parameters or test-data sets.
81. Final Summary
Parameterized Tests are an important part of modern Selenium automation frameworks because they allow the same test logic to execute with different input values.
TestNG provides @Parameters for named configuration values and @DataProvider for multiple sets of test data. These features can be used for browser selection, environment URLs, login credentials, search values, user roles, product data, expected results, and many other testing scenarios.
Parameterized tests become especially powerful when combined with Selenium WebDriver, Page Object Model, DriverFactory, external test data, assertions, Maven, CI/CD, parallel execution, and reporting tools.
A well-designed parameterized framework keeps test logic reusable, test data manageable, browser and environment configuration flexible, and test execution scalable.
82. Course Resources
Learn more about Selenium automation, TestNG, framework development, and practical automation testing:
Final Takeaway: Parameterized testing helps Selenium automation frameworks become reusable, scalable, maintainable, and data-driven. Use @Parameters for configuration-oriented values and @DataProvider when the same test needs to execute against multiple data sets.